package com.ruoyi.web.controller.system.assets;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.config.HeadNum;
import com.ruoyi.common.core.domain.Ajax2Result;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.GetSessionValueUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.assets.FzBusinessFloor;
import com.ruoyi.system.domain.communiry.FzSCommunity;
import com.ruoyi.system.service.assets.IFzBusinessFloorService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpSession;

/**
 * 楼栋信息Controller
 * 
 * @author ruoyi
 * @date 2020-02-28
 */
@Controller
@RequestMapping("/system/floor")
public class FzBusinessFloorController extends BaseController
{
    private String prefix = "system/assets/floor";

    @Autowired
    private IFzBusinessFloorService fzBusinessFloorService;

    @RequiresPermissions("system:floor:view")
    @GetMapping()
    public String floor()
    {
        return prefix + "/floor";
    }

    /**
     * 查询楼栋信息列表
     */
    @RequiresPermissions("system:floor:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HttpSession session,FzBusinessFloor fzBusinessFloor)
    {
        startPage();
        String communityId=((FzSCommunity)session.getAttribute("ycommunity")).getCommunityId();
        fzBusinessFloor.setcId(communityId);
        List<FzBusinessFloor> list = fzBusinessFloorService.selectFzBusinessFloorList(fzBusinessFloor);
        return getDataTable(list);
    }

    /**
     * 导出楼栋信息列表
     */
    @RequiresPermissions("system:floor:export")
    @Log(title = "楼栋信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FzBusinessFloor fzBusinessFloor)
    {
        List<FzBusinessFloor> list = fzBusinessFloorService.selectFzBusinessFloorList(fzBusinessFloor);
        ExcelUtil<FzBusinessFloor> util = new ExcelUtil<FzBusinessFloor>(FzBusinessFloor.class);
        return util.exportExcel(list, "floor");
    }

    /**
     * 新增楼栋信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存楼栋信息
     */
    @RequiresPermissions("system:floor:add")
    @Log(title = "楼栋信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FzBusinessFloor fzBusinessFloor)
    {
        fzBusinessFloor.setFloorId(HeadNum.FLOOR_HEAD+ DateUtils.getTimeMillis());
        fzBusinessFloor.setcId(GetSessionValueUtil.getCommunity());
        fzBusinessFloor.setCreateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(fzBusinessFloorService.insertFzBusinessFloor(fzBusinessFloor));
    }

    /**
     * 修改楼栋信息
     */
    @GetMapping("/edit/{floorId}")
    public String edit(@PathVariable("floorId") String floorId, ModelMap mmap)
    {
        FzBusinessFloor fzBusinessFloor = fzBusinessFloorService.selectFzBusinessFloorById(floorId);
        mmap.put("fzBusinessFloor", fzBusinessFloor);
        return prefix + "/edit";
    }

    /**
     * 修改保存楼栋信息
     */
    @RequiresPermissions("system:floor:edit")
    @Log(title = "楼栋信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FzBusinessFloor fzBusinessFloor)
    {
        fzBusinessFloor.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(fzBusinessFloorService.updateFzBusinessFloor(fzBusinessFloor));
    }

    /**
     * 删除楼栋信息
     */
    @RequiresPermissions("system:floor:remove")
    @Log(title = "楼栋信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fzBusinessFloorService.deleteFzBusinessFloorByIds(ids));
    }


    @GetMapping("/floorById/{floorId}")
    @ResponseBody
    public Ajax2Result selectfloorById(@PathVariable("floorId") String floorId)
    {
        FzBusinessFloor fzBusinessFloor = fzBusinessFloorService.selectFzBusinessFloorById(floorId);
        List<FzBusinessFloor> fzBusinessFloors= Arrays.asList(fzBusinessFloor);
        return Ajax2Result.success(fzBusinessFloors);
    }
}
